<template>
  <el-dialog title="重置密码" :visible.sync="dialogFormVisible" width="30%">
    <el-form :model="formModel" status-icon :rules="formRules" ref="formRef" :label-width="formLabelWidth">
      <el-form-item label="账号" prop="userId">
        <el-input prefix-icon="el-icon-user" placeholder="请输入账号"
                  v-model.number="formModel.userId"/>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input prefix-icon="el-icon-lock" show-password placeholder="请输入密码"
                  v-model.trim="formModel.password"/>
      </el-form-item>
      <el-form-item prop="confirmPass" label="确认密码">
        <el-input prefix-icon="el-icon-lock" show-password placeholder="请确认密码"
                  v-model.trim="formModel.confirmPass"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {mapActions, mapState} from "vuex";

export default {
  name: "ResetPasswordForm",
  data() {
    // 二次密码验证
    const validatePassword = (rule, value, callback) => {
      if (value !== this.formModel.password) {
        callback(new Error('两次密码不一致'));
      } else {
        callback();
      }
    }
    return {
      formLabelWidth: '100px',
      // 表单收集的数据
      formModel: {
        userId: '',
        password: '',
        confirmPass: '',
      },
      // 表单输入规则
      formRules: {
        userId: [
          {type: 'number', required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {type: 'string', required: true, message: '请输入密码', trigger: 'blur'},
          {min: 5, max: 12, message: '长度为 5 到 12 个字符', trigger: 'blur'},
        ],
        confirmPass: [
          {type: 'string', required: true, message: '请确认新密码', trigger: 'blur'},
          {validator: validatePassword, trigger: 'blur'},
        ]
      },
    }
  },
  computed: {
    ...mapState('login', ['dialogFormVisible']),
    // 将表单数据封装成user对象
    user() {
      return {
        userId: this.formModel.userId,
        password: this.formModel.password,
      }
    }
  },
  methods: {
    ...mapActions('login', ['modifyDialogFormVisible']),
    resetForm() {
      this.$refs.formRef.resetFields(); // 重置表单为初始值
    },
    handleCancel() {
      this.modifyDialogFormVisible();
      this.resetForm();
    },
    handleConfirm() {
      // 我猜validate方法是el-form的方法
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const url = '/admin/index/password';
          this.axios.put(url, this.user).then(resp => {
            if (resp.code === 200) {
              // 响应成功
              this.modifyDialogFormVisible();
              this.resetForm();
              this.$message({
                type: "success",
                message: '密码重置成功',
                center: true,
              });
            } else {
              this.modifyDialogFormVisible();
              // 响应失败
              this.$message({
                type: "error",
                message: resp.message,
                showClose: true,
                center: true,
              })
            }
          })
        } else {
          console.log('输入有误，请检查！');
          return false;
        }
      });
    }
  },
}
</script>

<style scoped>
.el-input {
  width: 80%;
}
</style>